package com.tjing.bussiness.controller;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.tjing.bussiness.model.QhUser;
import com.tjing.frame.controller.CommonController;
import com.tjing.frame.controller.ToolController;
import com.tjing.frame.services.DbServices;
import common.tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by cwp on 2017/8/19.
 */

@Component
public class TimerTask {
    private static org.apache.log4j.Logger logger= org.apache.log4j.Logger.getLogger(AppApiController.class);

    @Autowired
    private DbServices dbservices;

    @Scheduled(cron = "0 10 0 * * ?")
    public void test1(){
        logger.info("2017/09/29测试触发");
        //StringBuffer url = request.getRequestURL();
        //String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getContextPath()).append("/").toString();

        //tool.sendPost("http://127.0.0.1:6161/qh/appapi/appapi.htmls", "fun=addCommission&uid=0");
        //tool.sendGet("http://www.qianhuu.com/qh/appapi/appapi.htmls", "fun=addCommission&uid=0");

        /*List<JSONObject> jsonObjects= Lists.newArrayList();
        JSONObject jsonObject = new JSONObject();

        SimpleDateFormat fmtTime=new SimpleDateFormat("yyyy-MM-dd");
        Date date= ToolController.addDay(new Date(), -1);
        String strDate=fmtTime.format(date);

        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strnowTime= time.format(new Date());

        HashMap<Integer, Object> intParamMap = new HashMap();

        String hql="from QhUser";
        List<QhUser> qhUsers = dbservices.findListByHql(hql, new HashMap());
        for(QhUser qhUser:qhUsers){
            hql = "select sum(CashMoney) from qh_user_profit where CreateUser=? and DATE_FORMAT(CreateDateTime,'%Y-%m-%d')=?";
            Integer i = 0;
            intParamMap.clear();
            intParamMap.put(i++, qhUser.getMobile());
            intParamMap.put(i++, strDate);
            Object Omoney = dbservices.findUniqueBySql(hql, intParamMap);

            String inviter=qhUser.getInviter();

            for(Integer y=0;y<9;y++){
                if(inviter.equals("")){
                    break;
                }
                i=0;
                intParamMap.clear();
                intParamMap.put(i++, inviter);
                hql="from QhUser where mobile=?";
                List<QhUser> qhUsers1 = dbservices.findListByHql(hql, intParamMap);
                if(qhUsers1.size()>0){
                    inviter=qhUsers1.get(0).getInviter();

                    if(qhUsers1.get(0).getLeav()>y) {
                        jsonObject.put("uid", qhUsers1.get(0).getId());

                        Double per=0.0;
                        switch (y){
                            case 0:
                                per=0.3;
                                break;
                            case  1:
                                per=0.08;
                                break;
                            case 2:
                                per=0.08;
                                break;
                            case 3:
                                per=0.12;
                                break;
                            case 4:
                                per=0.12;
                                break;
                            case 5:
                                per=0.09;
                                break;
                            case 6:
                                per=0.08;
                                break;
                            case 7:
                                per=0.07;
                                break;
                            case 8:
                                per=0.06;
                                break;
                            default:
                                break;
                        }
                        Double cashMoney=Omoney==null?0.0:Double.valueOf(Omoney.toString());
                        jsonObject.put("cashMoney", cashMoney * per);
                        jsonObject.put("note", "");
                        jsonObject.put("createUser", qhUser.getMobile());
                        jsonObject.put("createDateTime", strnowTime);
                        jsonObject.put("modifyUser", qhUser.getMobile());
                        jsonObject.put("modifyDateTime", strnowTime);
                        jsonObject.put("state", 99);
                        jsonObjects.clear();
                        jsonObjects.add(jsonObject);
                        dbservices.saveOrupdateRecords("QhUserCommission", jsonObjects, null, false);
                    }
                }
            }

        }*/
    }

}
